package com.piesat.quake.common.mqtt.decoder.common;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class CommonEncoder {

    public static byte[] encoder(int length, Object... values) {
        ByteBuf byteBuf = Unpooled.buffer(length);
        for (Object value : values) {
            if (value instanceof String) {
                byteBuf.writeBytes(((String) value).getBytes());
            } else if (value instanceof Float) {
                byteBuf.writeFloatLE((Float) value);
            } else if (value instanceof Double) {
                byteBuf.writeDoubleLE((Double) value);
            } else if (value instanceof Integer) {
                byteBuf.writeIntLE((Integer) value);
            } else if (value instanceof Byte) {
                byteBuf.writeByte((Byte) value);
            } else if (value instanceof Long) {
                byteBuf.writeLongLE((Long) value);
            } else if (value instanceof Short) {
                byteBuf.writeShortLE((Short) value);
            }
        }
        return byteBuf.array();

    }
}
